<script setup lang="ts">
import { ref } from "vue";
import { useClass } from "./hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
// import type { FormInstance } from "element-plus";
import Search from "@iconify-icons/ep/search";
import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import AddFill from "@iconify-icons/ri/add-circle-line";
defineOptions({
  name: "Class"
});
const ClassToRef = ref();
const {
  form,
  rules,
  loading,
  columns,
  dataList,
  loadingTitle,
  pagination,
  MGetAllOption,
  updateClassDto,
  updatePanelVisible,
  onSearch,
  updateClass,
  ClosePanelVisible,
  handleDelete,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  addClass,
  updateNumber
} = useClass();
</script>

<template>
  <div class="main">
    <el-form :inline="true" :model="form" class="bg-bg_color w-[99/100] pl-4 pt-4">
      <el-form-item label="班级名称">
        <el-input v-model="form.ClassName" placeholder="请输入班级名称" clearable class="!w-[180px]" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.remark" placeholder="请输入备注" clearable class="!w-[180px]" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="useRenderIcon(Search)" :loading="loading" @click="onSearch">
          搜索
        </el-button>
      </el-form-item>
    </el-form>

    <el-dialog v-model="updatePanelVisible" :title="loadingTitle + '班级'" @close="ClosePanelVisible(ClassToRef)"
      width="30%" center :close-on-click-modal="false">
      <el-form ref="ClassToRef" label-position="left" label-width="100px" :rules="rules" :model="updateClassDto"
        style="max-width: 460px">
        <el-form-item label="班级编号:" prop="ClassNO">
          <el-input v-model="updateClassDto.ClassNO" :disabled="Boolean(updateClassDto.ClassId)" class="w-220" />
        </el-form-item>
        <el-form-item label="班级名称:" prop="ClassName">
          <el-input v-model="updateClassDto.ClassName" maxlength="15" show-word-limit class="w-220" />
        </el-form-item>
        <!-- <el-form-item label="机构组织:" prop="OrganizationId">
            <el-select v-model="updateClassDto.OrganizationId" value-key="organizationId" class="w-220">
              <el-option v-for="item in MGetAllOption" :key="item.value" :label="item.organizationName" :value="item" />
            </el-select>
          </el-form-item> -->
        <el-form-item label="用户数量:" prop="UserNum">
          <el-input type="number" v-model="updateClassDto.UserNum" min="0" @change="updateNumber(ClassToRef)"
            class="w-220" @input="if (updateClassDto.UserNum > 9999) updateClassDto.UserNum = 9999;" />
        </el-form-item>
        <el-form-item label="到期时间" prop="ExpirationTime">
          <el-date-picker v-model="updateClassDto.ExpirationTime" type="datetime" placeholder="请选择到期时间" class="w-220" />
        </el-form-item>
        <el-form-item label="备注:" prop="Remark">
        <el-input v-model="updateClassDto.Remark" type="textarea" maxlength="50" show-word-limit />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="addClass(ClassToRef)">
            保存
          </el-button>
          <el-button @click="ClosePanelVisible(ClassToRef)">取消</el-button>
        </span>
      </template>
    </el-dialog>

    <PureTableBar title="班级列表" @refresh="onSearch()" class="h100-60">
      <template #buttons>
        <p class="text-red-500 absolute left-[120px] leading-7 text-sm">注：双击查看详情</p>
        <el-button type="primary" :icon="useRenderIcon(AddFill)" @click="updateClass(0)">
          添加班级
        </el-button>
      </template>
      <template v-slot="{ size, checkList }">
        <pure-table border align-whole="center" showOverflowTooltip table-layout="auto" :loading="loading" :size="size"
          :data="dataList" :columns="columns" :checkList="checkList" :pagination="pagination" max-height="650"
          :paginationSmall="size === 'small' ? true : false" @cell-dblclick="updateClass" :header-cell-style="{
            background: 'var(--el-table-row-hover-bg-color)',
            color: 'var(--el-text-color-primary)'
          }" @selection-change="handleSelectionChange" @size-change="handleSizeChange"
          @current-change="handleCurrentChange">
          <template #operation="{ row }">
            <el-button class="reset-margin" link type="primary" :size="size" :icon="useRenderIcon(EditPen)"
              @click="updateClass(row)">
              修改
            </el-button>
            <el-button class="reset-margin" link type="danger" :size="size" @click="handleDelete(row)"
              :icon="useRenderIcon(Delete)">
              删除
            </el-button>
            <!-- <el-popconfirm title="是否确认删除?" @confirm="handleDelete(row)">
                <template #reference>
                  <el-button class="reset-margin" link type="danger" :size="size" :icon="useRenderIcon(Delete)">
                    删除
                  </el-button>
                </template>
              </el-popconfirm> -->
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

:deep(.el-form-item) {
  margin-bottom: 15px;
}
</style>
